<input id="modify_water" type="hidden" name="modify_water" value="0" />

<?php

global $wpdb, $post;
        
$post_id = $post->ID;
$type_id = 1;
      
$basic_devices_list = $wpdb->get_results($wpdb->prepare("select * from bpa_inventory_item where type_id = %d and custom = 0", $type_id));

$basic_devices_data = $wpdb->get_results($wpdb->prepare(
        "select bd.*, it.type_id 
        from bpa_saving_basic_device bd
        left join bpa_inventory_item it
        on bd.item_id = it.id
        where bd.post_id = %d
        and it.type_id = %d", $post_id, $type_id));
        
?>

<?php   if($basic_devices_data) {
    
            $c = 0;
            foreach ($basic_devices_data as $basic_device) {
                
                $basic_device_id = $basic_device->id;
                $basic_device_item_id = $basic_device->item_id;
                $basic_device_desc = $basic_device->description;
                $basic_device_average_use = $basic_device->average_use;
                $basic_device_consumption_per_time = $basic_device->consumption_per_time;
                
                $saving_items_list_query = "select * from bpa_saving_item where item_id = %d";
                $saving_items_list = $wpdb->get_results($wpdb->prepare($saving_items_list_query, $basic_device_item_id));
                
                $saving_devices_query = "select * from bpa_saving_alternative_device where basic_device_id = %d";
                $saving_devices = $wpdb->get_results($wpdb->prepare($saving_devices_query, $basic_device_id));
                
                $c++;
                
?>
             
                <div class="basic-device" id="water_basic_device_<?php echo $basic_device_id; ?>">
                    
                    <div class="basic-device-header">
                        <strong>Equipo b&aacute;sico <?php echo $c; ?></strong>
                        <a class="remove-basic-device button" href="#" title="Remover dispositivo b&aacute;sico">-</a>
                    </div>
                    
                    <div class="basic-device-form">
                    
                        <table class="form-table">
                            <tr class="input-hidden">
                                <td colspan="2">
                                    <input type="hidden" name="water_basic_device_counter[]" value="1" class="saving-device-counter-data" />
                                    <input type="hidden" name="water_saving_device_array[]" value="" class="saving-device-values-data" />
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <p class="label">Ingrese el monto del ahorro en texto:</p>
                                </td>
                                <td>
                                    <input name="water_basic_device_description[]" type="text" value="<?php echo $basic_device_desc; ?>" size="40" aria-required="true"/>
                                    <p class="description"><italic>Por ej. "No calculable" o,"Ninguno" o "Variable"</italic></p>
                                </td>  
                            </tr>
                            <tr>
                                <td>
                                    <p class="label">Equipo b&aacute;sico:</p>
                                </td>
                                <td>
                                    <select class="basic-device-selector" name="water_basic_device[]">
                                        <option value="0">--Seleccione un equipo base--</option>
                                        <?php
                                            foreach($basic_devices_list as $item){
                                                $selected = $item->id == $basic_device_item_id ? ' selected="selected" ' : '';
                                                $name = $item->full_name ? $item->full_name : $item->name;
                                        ?>
                                                <option <?php echo $selected;?> value="<?php echo $item->id;?>"><?php echo $name;?></option>
                                                <?php
                                            }
                                        ?>
                                    </select>
                                </td>  
                            </tr>
                            <tr>
                                <td>
                                    <p class="label">Uso del equipo al mes:</p>
                                </td>
                                <td>
                                    <input name="water_basic_device_usage[]" type="text" class="mask_usage" value="<?php echo $basic_device_average_use; ?>" size="35" aria-required="true"/>
                                    <p class="description"><italic>En minutos, veces usado, etc.</italic></p>
                                </td>  
                            </tr>
                            <tr>
                                <td>
                                    <p class="label">Consumo del equipo en metros c&uacute;bicos:</p>
                                </td>
                                <td>
                                    <input name="water_basic_device_consumption[]" type="text" class="mask_decimal" value="<?php echo $basic_device_consumption_per_time; ?>" size="35" aria-required="true"/>
                                    <p class="description"><italic>Por minuto, por vez usado, etc.</italic></p>
                                </td>  
                            </tr>
                            <tr>
                                <td>
                                    <p class="label">Dispositivos de ahorro:</p>
                                </td>
                                <td>
                                    <table class="saving-devices-table" width="100%">
                                        <thead>
                                            <tr>
                                                <th width="49%">Nombre de dispositivo</th>
                                                <th width="49%">Porcentaje de ahorro (En decimales):</th>
                                                <th width="2%"></th>
                                            </tr>
                                        </thead>
                                        <?php
                                            if ( $saving_devices ) :
                                                foreach ( $saving_devices as $device ) {
                                        ?>
                                                    <tr class="saving-device-data-row">
                                                        <td>
                                                            <select class="saving-device-selector">
                                                                <option value="0">--Seleccione un dispositivo alterno--</option>
                                                                <?php
                                                                    foreach($saving_items_list as $item){
                                                                        $selected = $item->id == $device->saver_item_id ? ' selected="selected" ' : '';
                                                                ?>
                                                                        <option <?php echo $selected;?> value="<?php echo $item->id;?>"><?php echo $item->name;?></option>
                                                                        <?php
                                                                    }
                                                                ?>
                                                            </select>
                                                        </td>
                                                        <td><input type="text" class="saving-device-ratio widefat mask_decimal" value="<?php echo $device->saving_ratio; ?>" /></td>
                                                        <td><a class="button remove-saving-device" href="#">-</a></td>
                                                    </tr>
                                        <?php   }
                                            else :
                                                // show a blank one
                                        ?>
                                                <tr class="saving-device-data-row">
                                                    <td>
                                                        <select class="saving-device-selector">
                                                            <option value="0">--Seleccione un dispositivo alterno--</option>
                                                            <?php
                                                                foreach($saving_items_list as $item){
                                                                    $selected = $item->id == $device->saver_item_id ? ' selected="selected" ' : '';
                                                            ?>
                                                                    <option <?php echo $selected;?> value="<?php echo $item->id;?>"><?php echo $item->name;?></option>
                                                                    <?php
                                                                }
                                                            ?>
                                                        </select>
                                                    </td>
                                                    <td><input type="text" class="saving-device-ratio widefat mask_decimal" value="" /></td>
                                                    <td><a class="button remove-saving-device" href="#">-</a></td>
                                                </tr>
                                        <?php endif; ?>
                                        <!-- empty hidden one for jQuery -->
                                        <tr class="saving-device-empty-row">
                                            <td>
                                                <select class="saving-device-selector">
                                                    <option value="0">--Seleccione un dispositivo alterno--</option>
                                                    <?php
                                                        foreach($saving_items_list as $item){
                                                    ?>
                                                            <option value="<?php echo $item->id;?>"><?php echo $item->name;?></option>
                                                            <?php
                                                        }
                                                    ?>
                                                </select>
                                            </td>
                                            <td><input type="text" class="saving-device-ratio widefat mask_decimal" value="" /></td>
                                            <td><a class="remove-saving-device button" href="#">-</a></td>
                                        </tr>
                                        <tr class="add-saving-device-row">
                                            <td colspan="3">
                                                <a class="add-saving-device button" href="#">Agregar nuevo dispositivo alterno</a>
                                            </td>
                                        </tr>

                                    </table>
                                </td>  
                            </tr>
                        </table>

                    </div>
                    
                </div>

<?php                
            }
    
        } else {
 ?>          

            <div class="basic-device">
                
                <div class="basic-device-header">
                    <strong>Equipo b&aacute;sico nuevo</strong>
                </div>
                
                <div class="basic-device-form">
                    
                    <table class="form-table">
                        <tr class="input-hidden">
                            <td colspan="2">
                                <input type="hidden" name="water_basic_device_counter[]" value="1" class="saving-device-counter-data" />
                                <input type="hidden" name="water_saving_device_array[]" value="" class="saving-device-values-data" />
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <p class="label">Ingrese el monto del ahorro en texto:</p>
                            </td>
                            <td>
                                <input name="water_basic_device_description[]" type="text" value="" size="40" aria-required="true"/>
                                <p class="description"><italic>Por ej. "No calculable" o,"Ninguno" o "Variable"</italic></p>
                            </td>  
                        </tr>
                        <tr>
                            <td>
                                <p class="label">Equipo b&aacute;sico:</p>
                            </td>
                            <td>
                                <select class="basic-device-selector" name="water_basic_device[]">
                                    <option value="0">--Seleccione un equipo base--</option>
                                    <?php foreach($basic_devices_list as $item){ 
                                            $name = $item->full_name ? $item->full_name : $item->name;
                                    ?>
                                            <option <?php echo $selected;?> value="<?php echo $item->id;?>"><?php echo $name;?></option>
                                    <?php } ?>
                                </select>
                            </td>  
                        </tr>
                        <tr>
                            <td>
                                <p class="label">Uso del equipo al mes:</p>
                            </td>
                            <td>
                                <input name="water_basic_device_usage[]" type="text" class="mask_usage" value="" size="35" aria-required="true"/>
                                <p class="description"><italic>En minutos, veces usado, etc.</italic></p>
                            </td>  
                        </tr>
                        <tr>
                            <td>
                                <p class="label">Consumo del equipo en metros c&uacute;bicos:</p>
                            </td>
                            <td>
                                <input name="water_basic_device_consumption[]" type="text" class="mask_decimal" value="" size="35" aria-required="true"/>
                                <p class="description"><italic>Por minuto, por vez usado, etc.</italic></p>
                            </td>  
                        </tr>
                        <tr>
                            <td>
                                <p class="label">Dispositivos de ahorro:</p>
                            </td>
                            <td>
                                <table class="saving-devices-table" width="100%">
                                    <thead>
                                        <tr>
                                            <th width="49%">Nombre de dispositivo</th>
                                            <th width="49%">Porcentaje de ahorro (En decimales):</th>
                                            <th width="2%"></th>
                                        </tr>
                                    </thead>

                                    <tr class="saving-device-data-row">
                                        <td>
                                            <select class="saving-device-selector">
                                                <option value="0">--Seleccione un dispositivo alterno--</option>
                                            </select>
                                        </td>
                                        <td><input type="text" class="saving-device-ratio widefat mask_decimal" value="" /></td>
                                        <td><a class="button remove-saving-device" href="#">-</a></td>
                                    </tr>
                                    
                                    <!-- empty hidden one for jQuery -->
                                    <tr class="saving-device-empty-row">
                                        <td>
                                            <select class="saving-device-selector">
                                                <option value="0">--Seleccione un dispositivo alterno--</option>
                                            </select>
                                        </td>
                                        <td><input type="text" class="saving-device-ratio widefat mask_decimal" value="" /></td>
                                        <td><a class="remove-saving-device button" href="#">-</a></td>
                                    </tr>
                                    <tr class="add-saving-device-row">
                                        <td colspan="3">
                                            <a class="add-saving-device button" href="#">Agregar nuevo dispositivo alterno</a>
                                        </td>
                                    </tr>
                                    
                                </table>
                            </td>  
                        </tr>
                    </table>
                    
                </div>

            </div>
            
 <?php           
        } 
    
    ?>

<p id="water-add-basic-device-row">
    <a id="water-add-basic-device" class="button" href="#">Agregar nuevo dispositivo b&aacute;sico</a>
</p>

<div id="water-basic-device-empty-row" class="basic-device-empty-row">
    
    <div class="basic-device-header">
        <strong>Equipo b&aacute;sico nuevo</strong>
        <a class="remove-basic-device button" href="#" title="Remover dispositivo b&aacute;sico">-</a>
    </div>

    <div class="basic-device-form">

        <table class="form-table">
            <tr class="input-hidden">
                <td colspan="2">
                    <input type="hidden" name="water_basic_device_counter[]" value="0" class="saving-device-counter-data" />
                    <input type="hidden" name="water_saving_device_array[]" value="" class="saving-device-values-data" />
                    
                </td>
            </tr>
            <tr>
                <td>
                    <p class="label">Ingrese el monto del ahorro en texto:</p>
                </td>
                <td>
                    <input name="water_basic_device_description[]" type="text" value="" size="40" aria-required="true"/>
                    <p class="description"><italic>Por ej. "No calculable" o,"Ninguno" o "Variable"</italic></p>
                </td>  
            </tr>
            <tr>
                <td>
                    <p class="label">Equipo b&aacute;sico:</p>
                </td>
                <td>
                    <select class="basic-device-selector" name="water_basic_device[]">
                        <option value="0">--Seleccione un equipo base--</option>
                        <?php
                            foreach($basic_devices_list as $item){
                                $name = $item->full_name ? $item->full_name : $item->name;
                        ?>
                                <option value="<?php echo $item->id;?>"><?php echo $name;?></option>
                        <?php } ?>
                    </select>
                </td>  
            </tr>
            <tr>
                <td>
                    <p class="label">Uso del equipo al mes:</p>
                </td>
                <td>
                    <input name="water_basic_device_usage[]" type="text" class="mask_decimal" value="" size="35" aria-required="true"/>
                    <p class="description"><italic>En minutos, veces usado, etc.</italic></p>
                </td>  
            </tr>
            <tr>
                <td>
                    <p class="label">Consumo del equipo en metros c&uacute;bicos:</p>
                </td>
                <td>
                    <input name="water_basic_device_consumption[]" type="text" class="mask_decimal" value="" size="35" aria-required="true"/>
                    <p class="description"><italic>Por minuto, por vez usado, etc.</italic></p>
                </td>  
            </tr>
            <tr>
                <td>
                    <p class="label">Dispositivos de ahorro:</p>
                </td>
                <td>
                    <table class="saving-devices-table" width="100%">
                        <thead>
                            <tr>
                                <th width="49%">Nombre de dispositivo</th>
                                <th width="49%">Porcentaje de ahorro (En decimales):</th>
                                <th width="2%"></th>
                            </tr>
                        </thead>
                        <tr class="saving-device-data-row">
                            <td>
                                <select class="saving-device-selector">
                                    <option value="0">--Seleccione un dispositivo alterno--</option>
                                </select>
                            </td>
                            <td><input type="text" class="saving-device-ratio widefat mask_decimal" value="" /></td>
                            <td><a class="button remove-saving-device" href="#">-</a></td>
                        </tr>
                        <tr class="saving-device-empty-row">
                            <td>
                                <select class="saving-device-selector">
                                    <option value="0">--Seleccione un dispositivo alterno--</option>
                                </select>
                            </td>
                            <td><input type="text" class="saving-device-ratio widefat mask_decimal" value="" /></td>
                            <td><a class="remove-saving-device button" href="#">-</a></td>
                        </tr>
                        <tr class="add-saving-device-row">
                            <td colspan="3">
                                <a class="add-saving-device button" href="#">Agregar nuevo dispositivo alterno</a>
                            </td>
                        </tr>

                    </table>
                </td>  
            </tr>
        </table>

    </div>

</div>
    

<?php   ?>

<p><input type="submit" class="metabox_submit" value="Guardar" /></p>
